package com.edgclub.edg.utils.GlideUtil;

import android.content.Context;
import android.os.Environment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.module.GlideModule;
import com.bumptech.glide.request.target.ViewTarget;
import com.edgclub.edg.R;
import com.edgclub.edg.utils.util.FileUtility;

import java.io.File;
import java.io.IOException;

public class MyGlideModule implements GlideModule  {
	
	@Override
	public void applyOptions(final Context context, GlideBuilder builder) {
		
		
		ViewTarget.setTagId(R.id.glide_tag_id);
		builder.setDiskCache(new DiskCache.Factory() {
			@Override
			public DiskCache build() {
				File cacheLocation = new File(context.getExternalCacheDir(),
						"edg_dir");
				cacheLocation.mkdirs();
				if(!cacheLocation.exists()){
					try {
						FileUtility.makeDirectory(Environment.getExternalStorageDirectory() + "/edg_dir");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				DiskCache diskCache = DiskLruCacheWrapper
						.get(cacheLocation, 250 * 1024 * 1024);
				return diskCache;
			}
		});
		builder.setMemoryCache(new LruResourceCache(3 * 1024 * 1024));
		builder.setBitmapPool(new LruBitmapPool(3 * 1024 * 1024));
	}

	@Override
	public void registerComponents(Context context, Glide glide) {
//		glide.register(String.class, InputStream.class,  new TbxImageLoader.Factory());
	}

}
